// (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
// Use is subject to license terms.

package demo.basic.hp.registry;

import org.systinet.uddi.InvalidParameterException;
import org.systinet.uddi.client.v3.UDDIException;
import org.systinet.uddi.client.v3.UDDIPublishStub;
import org.systinet.uddi.client.v3.UDDISecurityStub;
import org.systinet.uddi.client.v3.UDDI_Publication_PortType;
import org.systinet.uddi.client.v3.UDDI_Security_PortType;
import org.systinet.uddi.client.v3.struct.AccessPoint;
import org.systinet.uddi.client.v3.struct.AuthToken;
import org.systinet.uddi.client.v3.struct.BindingTemplate;
import org.systinet.uddi.client.v3.struct.BusinessService;
import org.systinet.uddi.client.v3.struct.Get_authToken;
import org.systinet.uddi.client.v3.struct.BusinessDetail;
import org.systinet.uddi.client.v3.struct.BusinessEntityArrayList;
import org.systinet.uddi.client.v3.struct.BusinessEntity;
import org.systinet.uddi.client.v3.struct.Save_business;
import org.systinet.uddi.client.v3.struct.Description;
import org.systinet.uddi.client.v3.struct.Name;
import org.systinet.uddi.client.v3.struct.Save_tModel;
import org.systinet.uddi.client.v3.struct.ServiceInfo;
import org.systinet.uddi.client.v3.struct.TModel;
import org.systinet.uddi.client.v3.struct.TModelDetail;
import org.systinet.uddi.client.v3.struct.TModelInstanceInfo;

import javax.xml.soap.SOAPException;
import java.util.Iterator;

public class UDDIPublishing {

    public static final String URL_PUBLISHING = "http://localhost:8089/uddi/publishing";
    public static final String URL_SECURITY = "http://localhost:8089/uddi/security";

    private static final String USER_NAME = "demo_john";
    private static final String USER_PASSWORD = "demo_john";
    private UDDI_Publication_PortType publishing = null;
    private UDDI_Security_PortType security = null;
    private AuthToken authToken = null;

    public UDDIPublishing() {
    	try {
			publishing = UDDIPublishStub.getInstance(URL_PUBLISHING);
			security = UDDISecurityStub.getInstance(URL_SECURITY);
			authToken = security.get_authToken(new Get_authToken(USER_NAME, USER_PASSWORD));
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (UDDIException e) {
			e.printStackTrace();
		} catch(InvalidParameterException e) {
			e.printStackTrace();
		}
    }

    public void publish2Registry() {
    	String businessKey = "uddi:systinet.com:demo:basic";
    	int count = 1;
    	String[] businessNames = new String[count];
    	String[] serviceNames = new String[count];
    	String[] tModelNames = new String[count];
    	String[] languageCodes = new String[count];
    	languageCodes[0] = null; // can set an array of language codes
    	
    	businessNames[0] = "Demo Business";
    	String businessDescription = "Demo Business Example"; //business description
    	
    	serviceNames[0] = "Demo Service"; //service name
    	String serviceDescription = "Demo Service Example"; //service description
    	
    	String bindingDescription = "Demo Binding Example"; //binding description
    	
    	tModelNames[0] = "Demo tModel"; //tmodel name
    	String tModelDescription = "Demo tModel Example"; //tmodel description
    	
    	BusinessEntity businessEntity = new BusinessEntity();
    	BusinessService businessService = new BusinessService();
    	BindingTemplate bindingTemplate = new BindingTemplate();
    	TModel tModel = new TModel();
    	System.out.println("Publishing business entity, service, binding and tModel instances...");
    	try {
			businessEntity.setBusinessKey(businessKey);
			businessEntity.addName(new Name(businessNames[0],languageCodes[0]));
			businessService.addDescription(new Description(businessDescription));
			
			businessEntity.addBusinessService(businessService);
	    	businessService.addName(new Name(serviceNames[0], languageCodes[0]));
	    	businessService.addDescription(new Description(serviceDescription));
	    	businessService.setBusinessKey(businessKey);
	    	
	    	businessService.addBindingTemplate(bindingTemplate);
	    	bindingTemplate.addDescription(new Description(bindingDescription));
	    	bindingTemplate.setAccessPoint(new AccessPoint("building Helios", "location"));
	    	tModel.addDescription(new Description(tModelDescription));
	    	tModel.setName(new Name(tModelNames[0],languageCodes[0]));
	    	
	    	Save_tModel save_tModel = new Save_tModel();
	    	save_tModel.addTModel(tModel);
	    	save_tModel.setAuthInfo(authToken.getAuthInfo());
	    	TModelDetail tModelDetail = publishing.save_tModel(save_tModel);
	    	for(Iterator<TModel> iter = tModelDetail.getTModelArrayList().iterator();iter.hasNext();) {
	    		String tModelKey = iter.next().getTModelKey();
	    		TModelInstanceInfo tModelInstInfo = new TModelInstanceInfo(tModelKey);
		    	bindingTemplate.addTModelInstanceInfo(tModelInstInfo);
		    	break;
	    	}
	    	
	    	Save_business save_business = new Save_business();
	    	save_business.addBusinessEntity(businessEntity);
	    	save_business.setAuthInfo(authToken.getAuthInfo());
	    	BusinessDetail businessDetail = publishing.save_business(save_business);
	    	printBusinessDetail(businessDetail);
	    	System.out.println("Done.");
		} catch (InvalidParameterException e1) {
			e1.printStackTrace();
		} catch (UDDIException e2) {
			e2.printStackTrace();
		}
    }
    
	public static void printBusinessDetail(BusinessDetail businessDetail) {
		System.out.println();
		BusinessEntityArrayList businessEntityArrayList = businessDetail
				.getBusinessEntityArrayList();
		int position = 1;
		for (Iterator iterator = businessEntityArrayList.iterator(); iterator
				.hasNext();) {
			BusinessEntity entity = (BusinessEntity) iterator.next();
			System.out.println("Business " + position + " : "
					+ entity.getBusinessKey());
			System.out.println(entity.toXML());
			System.out.println();
			System.out
					.println("********************************************************");
			position++;
		}
	}

    public static void main(String[] args) throws Exception {
        UDDIPublishing demo = new UDDIPublishing();
        demo.publish2Registry();
    }
}
